package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Cre_record_ga;
import com.power.sifa.correction.model.pageInfo.Cre_record_gaPageInfo;
import com.power.sifa.correction.api.ICre_record_gaFeign;
import com.power.sifa.correction.service.Cre_record_gaService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   公安推送记录控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Cre_record_gaController",tags={"公安推送记录"})
public class Cre_record_gaController implements ICre_record_gaFeign{

    @Resource
    private Cre_record_gaService cre_record_gaService;

    
    @Override
    @ApiOperation("查询公安推送记录分页列表")
    @SaCheckPermission("cre_record_ga_list")
    public ResponseDTO list(Cre_record_gaPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Cre_record_ga> page = this.cre_record_gaService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Cre_record_ga cre_record_ga : page) {
                cre_record_ga.geneText();
                list.add(cre_record_ga);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看公安推送记录")
    @SaCheckPermission("cre_record_ga_view")
    public ResponseDTO view(Integer id) {
        Assert.notNull(id, "id不能为空");
        Cre_record_ga cre_record_ga  = cre_record_gaService.getById(id);
        if (cre_record_ga != null) {
            cre_record_ga.geneText();
        }
        return ResponseDTO.succData(cre_record_ga);
    }

    @Override
    @ApiOperation("删除公安推送记录")
    @SaCheckPermission("cre_record_ga_del")
    public ResponseDTO delete(Integer id) {
        Assert.notNull(id, "id不能为空");
        cre_record_gaService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改公安推送记录")
    @SaCheckPermission("cre_record_ga_save")
    public ResponseDTO save(Cre_record_ga cre_record_ga) {
        Assert.notNull(cre_record_ga, "cre_record_ga不能为空");
        cre_record_gaService.saveOrUpdate(cre_record_ga);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除公安推送记录")
    @SaCheckPermission("cre_record_ga_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        cre_record_gaService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("cre_record_ga_list")
    public void output(HttpServletResponse response, Cre_record_gaPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "公安推送记录";
        String[] heads = {"序号","人员id","记录时间","拍摄纬度","拍摄经度","状态","照片","发生地点"};
        String[] fields = {"id","perid","rectime","latcamera","lngcamera","status","photo","addr"};
        Page<Cre_record_ga> page = this.cre_record_gaService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "公安推送记录";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","人员id","记录时间","拍摄纬度","拍摄经度","状态","照片","发生地点"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","perid","rectime","latcamera","lngcamera","status","photo","addr"};
        List<Cre_record_ga> list = (List<Cre_record_ga>)ExcelUtil.Import(fields, file, Cre_record_ga.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.cre_record_gaService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO recentRecordGa(String perId) {
        Assert.notNull(perId, "perId不能为空");
        try {
            return ResponseDTO.succData(this.cre_record_gaService.recentRecordGa(perId));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/cre_record_ga/recentRecordGa 查询最近公安记录->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "查询最近公安记录");
        }
    }

}
